package com.impetus.project.skilltest.server.service;

import java.util.HashMap;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.impetus.project.skilltest.client.stub.TestService;
import com.impetus.project.skilltest.common.Constants;
import com.impetus.project.skilltest.common.SkillTestException;
import com.impetus.project.skilltest.common.dataobject.Candidate;
import com.impetus.project.skilltest.common.dataobject.ObjectiveQuestion;
import com.impetus.project.skilltest.common.dataobject.ProgrammingQuestion;
import com.impetus.project.skilltest.common.dataobject.Question;
import com.impetus.project.skilltest.common.dataobject.ScheduledTest;
import com.impetus.project.skilltest.common.dataobject.TestTransData;
import com.impetus.project.skilltest.server.dao.ProgrammingQuestionDAO;
import com.impetus.project.skilltest.server.dao.ScheduledTestDAO;

public class TestServiceImpl extends RemoteServiceServlet implements TestService{
	
	// List of questions on a per user basis
	
	// Update ScheduledTest as required

	private static final long serialVersionUID = 1L;
	private static Logger logger = Logger.getLogger(TestServiceImpl.class.getName());
	private static HashMap<String, List<ProgrammingQuestion>> hmRunningProgrammingTests = null;
	private static HashMap<String, List<ObjectiveQuestion>> hmRunningObjectiveTests = null;

	public List<ScheduledTest> getScheduledTests(Candidate candidate)
	throws SkillTestException
	{
		SecurityServiceImpl.checkLogin(candidate);
		if(candidate.getIsAdmin()){
			return ScheduledTestDAO.getAllScheduledTests();
		}
		else{
			return ScheduledTestDAO.getScheduledTestByCandidateId(candidate.getCandidateid());
		}
	}
	
	public Question getNextQuestion(Candidate candidate, ScheduledTest test)
	throws SkillTestException
	{
		// TODO: remove test data and test output before sending to the client
		
		SecurityServiceImpl.checkLogin(candidate);
		
		String testKey = candidate.getCandidateid() + test.getTestid();
		
		if(Constants.QUESTION_TYPE.PROGRAMMING.equalsIgnoreCase(test.getqType())){
			if(hmRunningProgrammingTests == null){
				logger.log(Level.CONFIG, "Initializing running programming tests cache...");
				hmRunningProgrammingTests = new HashMap <String, List<ProgrammingQuestion>>();
			}
			
			if(!hmRunningProgrammingTests.containsKey(testKey)){
				{
					List<ProgrammingQuestion> alQuestion = 
						ProgrammingQuestionDAO.getProgrammingQuestionList(test.getqDomains(), 
							test.getComplexity(), test.getQuestionCount());
					hmRunningProgrammingTests.put(testKey, alQuestion);
				}
			}
			
			List<ProgrammingQuestion> listPQs = hmRunningProgrammingTests.get(testKey);
			if(listPQs.size() > test.getQuestionsAttempted()){
				ProgrammingQuestion pq = listPQs.get(test.getQuestionsAttempted());
			}
			else{
				throw new SkillTestException();
			}
		}
		
		
		
		
		
		
		return null;
	}
	
	public TestTransData evaluateAndSaveProgrammingQuestion(TestTransData transData)
	throws SkillTestException
	{
		//evaluateProgrammingQuestion(transData);
		return null;
	}
	
	public TestTransData evaluateProgrammingQuestion(TestTransData transData)
	throws SkillTestException
	{
		// will populate status and error text
		//Constants.ANSWER_STATUS.CORRECT
		return null;
	}
	
	public void saveUpdateScheduledTest(ScheduledTest scheduledTest)
	throws SkillTestException
	{
		try{
			ScheduledTestDAO.saveUpdateScheduledTest(scheduledTest);
		}
		catch(SkillTestException e){
			throw e;
		}catch(Exception ex){
			throw new SkillTestException(ex);
		}
	}
	
	public List<ScheduledTest> getScheduledTestByTestId ( String testId )
	throws SkillTestException
	{
		return ScheduledTestDAO.getScheduledTestByTestId(testId);
	}
	
}
